home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Demo ƒ / myPlatform demo ƒ / sHMovPlatform.p < prev    next >
Text File  |  1995-01-16  |  3KB  |  110 lines

  1. { Platform sprite, horizontally moving version (not faceless) }
  2.  
  3. unit sHMovPlatForm;
  4.  
  5. interface
  6.  
  7.     uses
  8. {$IFC UNDEFINED THINK_PASCAL}
  9.         Types, Quickdraw,
  10. {$ENDC}
  11.         SAT, sPlatForm, sMovPlatForm;
  12.  
  13.     procedure InitHMovPlatForm;
  14.     procedure SetupHMovPlatForm (me: SpritePtr);
  15.     procedure HandleHMovPlatForm (me: SpritePtr);
  16.     procedure HitHMovPlatForm (me, him: SpritePtr);
  17.  
  18. implementation
  19.  
  20.     procedure InitHMovPlatForm;
  21.         var
  22.             i: integer;
  23.     begin
  24. {platFace := GetFace(138); same as vertically moving version, we use the same!}
  25.     end;
  26.  
  27.     procedure SetupHMovPlatForm (me: SpritePtr);
  28.         var
  29.             r: Rect;
  30.             pol: PolyHandle;
  31.     begin
  32.         me^.speed.h := -1 + SATRand(2) * 2;
  33.         me^.kind := -2; {Enemy kind}
  34.         me^.face := platFace;
  35.         SetRect(me^.hotRect, 0, 3, 60, 20);
  36.         me^.task := @HandleHMovPlatform;
  37.         me^.hittask := @HitHMovPlatform;
  38.     end;
  39.  
  40.     procedure HandleHMovPlatForm (me: SpritePtr);
  41.     begin
  42.         me^.position.h := me^.position.h + me^.speed.h;
  43.         if me^.position.h < 40 then
  44.             me^.speed.h := 1;
  45.         if me^.position.h > gSAT.offSizeH - 100 then
  46.             me^.speed.h := -1;
  47.  
  48. {Move!}
  49.         if me^.speed.h = 0 then
  50.             if me^.position.h > gSAT.offSizeH div 2 then
  51.                 me^.speed.h := -1
  52.             else
  53.                 me^.speed.h := 1;
  54.  
  55.         me^.layer := -me^.position.v;
  56.     end;
  57.  
  58.     procedure HitHMovPlatForm;
  59.         var
  60.             mini, i, min: integer;
  61.             diff: array[1..4] of integer;
  62.     begin
  63.         if him^.Task <> @HandlePlatForm then  {check for HandleMovPlatForm too?}
  64.             begin
  65.                 diff[1] := -me^.hotRect2.top + (him^.hotRect2.bottom);{TtoB}
  66.                 diff[2] := -him^.hotRect2.top + (me^.hotRect2.bottom);{BtoT}
  67.                 diff[3] := -me^.hotRect2.left + (him^.hotRect2.right);{LtoR}
  68.                 diff[4] := -him^.hotRect2.left + (me^.hotRect2.right);{RtoL}
  69.                 mini := 0;
  70.                 min := 10000;
  71.                 for i := 1 to 4 do
  72.                     if min > diff[i] then
  73.                         begin
  74.                             min := diff[i];
  75.                             mini := i;
  76.                         end;
  77.                 case mini of
  78.                     1: {floor}
  79.                         begin
  80.                             him^.position.v := him^.position.v - diff[1] + 1;
  81.                             him^.position.h := him^.position.h + me^.speed.h; {or perhaps him^speed?}
  82.                             him^.kind := 10; {Signal to him, as if we used KindCollision}
  83.                             if him^.speed.v > 0 then
  84.                                 him^.speed.v := 0;
  85.                         end;
  86.                     2: {cieling}
  87.                         begin
  88.                             him^.position.v := him^.position.v + diff[2] + 1;{me^.position.v + 17}
  89. {No signal here}
  90.                             if him^.speed.v < 0 then
  91.                                 him^.speed.v := -him^.speed.v;
  92.                         end;
  93.                     3: {left}
  94.                         begin
  95.                             him^.position.h := him^.position.h - diff[3] - 1;
  96.                             him^.kind := 10; {Signal to him, as if we used KindCollision}
  97.                             if him^.speed.h > 0 then
  98.                                 him^.speed.h := -him^.speed.h;
  99.                         end;
  100.                     4: {right}
  101.                         begin
  102.                             him^.position.h := him^.position.h + diff[4] + 1;{me^.position.h + 100}
  103.                             him^.kind := 10; {Signal to him, as if we used KindCollision}
  104.                             if him^.speed.h < 0 then
  105.                                 him^.speed.h := -him^.speed.h;
  106.                         end;
  107.                 end;{case}
  108.             end; {if}
  109.     end; {HitHMovPlatForm}
  110. end.{of unit}